<template>
  <div>
    <van-picker
    title=""
    show-toolbar
    :columns="columns"
    :default-index="gender"
    @cancel ="$emit('cancel')"
    @confirm="onConfirm"
    @change='onchange'
    />
  </div>
</template>

<script>
import { updateUserName } from '@/api/user'
export default {
  name: 'userGender',
  props: {
    gender: {
      type: Number,
      required: true
    }
  },
  data () {
    return {
      columns: ['男', '女'],
      genderIndex: this.gender
    }
  },
  methods: {
    async onConfirm () {
      this.$toast.loading({
        message: '保存中...',
        forbidClick: true,
        duration: 0
      })
      try {
        await updateUserName({
        // 2.后端更新
          gender: this.genderIndex
        })
        // 3. 前端更新
        this.$emit('input', this.genderIndex)
        // 4.提示信息
        this.$toast.success('更新成功')
        // 5.弹层信息
        this.$emit('cancel')
      } catch (err) {
        this.$toast.fail('修改失败')
      }
    },
    // 监听滚动
    onchange (picker, value, index) {
      this.genderIndex = index
    }
  }
}
</script>

<style>

</style>
